package com.ruoyi.system.util.xfyun.utils;

public class SnowflakeIdGenerator {

    // 起始的时间戳（2023-01-01 00:00:00）
    private final static long START_TIMESTAMP = 1672531200000L;

    // 机器ID所占的位数
    private final static long WORKER_ID_BITS = 5L;

    // 数据中心ID所占的位数
    private final static long DATACENTER_ID_BITS = 5L;

    // 序列号所占的位数
    private final static long SEQUENCE_BITS = 12L;

    // 支持的最大机器ID
    private final static long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);

    // 支持的最大数据中心ID
    private final static long MAX_DATACENTER_ID = ~(-1L << DATACENTER_ID_BITS);

    // 机器ID向左移12位
    private final static long WORKER_ID_SHIFT = SEQUENCE_BITS;

    // 数据中心ID向左移17位（12+5）
    private final static long DATACENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;

    // 时间戳向左移22位（5+5+12）
    private final static long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATACENTER_ID_BITS;

    // 生成序列的掩码
    private final static long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS);

    // 工作机器ID(0~31)
    private long workerId;

    // 数据中心ID(0~31)
    private long datacenterId;

    // 毫秒内序列(0~4095)
    private long sequence = 0L;

    // 上次生成ID的时间戳
    private long lastTimestamp = -1L;

    public SnowflakeIdGenerator(long workerId, long datacenterId) {
        if (workerId > MAX_WORKER_ID || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", MAX_WORKER_ID));
        }
        if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", MAX_DATACENTER_ID));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    public synchronized long nextId() {
        long timestamp = timeGen();

        if (timestamp < lastTimestamp) {
            throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }

        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & SEQUENCE_MASK;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = timestamp;

        return ((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT) |
                (datacenterId << DATACENTER_ID_SHIFT) |
                (workerId << WORKER_ID_SHIFT) |
                sequence;
    }

    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    protected long timeGen() {
        return System.currentTimeMillis();
    }

    public static void main(String[] args) {
        SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
        for (int i = 0; i < 10; i++) {
            long id = idGenerator.nextId();
            System.out.println(id);
        }
    }
}
